* **************************************************************************** *
* Sierra Leone - Social Signaling and Childhood Immunization
* Correct Hand Out of Bracelets by Treatment Group
* **************************************************************************** *
/*
** Purpose:  Figure : Correct Hand Out of Bracelets by Treatment Groups


*/
* **************************************************************************** *
* **************************************************************************** *
	cap graph drop _all


	* Set the format/style of bar graphs
	cd   "${Replicate_SocialSignals_Out}/colorschemes/"
	grstyle init bracelets,  replace
	grstyle set plain
	grstyle set imesh, horizontal compact
	grstyle set legend, nobox
	grstyle set inten 90: bar

* **************************************************************************** *

	use  "${Replicate_SocialSignals_dtaInter}/Listing_Data.dta", clear


	keep if type_listing         == 1
	drop if clinic_attendlist    == 0 //exclude all children that do not attend bracelet clinics
	drop if immunization_listing == 0 //exclude all children that do not have any immunization

	** Merge in start dates

	sort  clinic
	merge m:m clinic using "${Replicate_SocialSignals_dtaInter}/MasterData_LaunchDates.dta", keep(3)

	* Merge in the ANC treatment status of clinics
	merge m:m clinic using "${Replicate_SocialSignals_dtaInter}/MasterData_PilotClinics.dta", gen(MergeANCInfo)


	** keep if children were born shortly before or after launch
	gen     start       = eligible + 5   
	drop if dob_listing < start
	gen     program     = start  - 61


	** Compute total number of immunizations received
	egen    num_vacc = rowtotal(bcg_listing penta1_listing penta2_listing penta3_listing measles1_listing), m
	replace num_vacc = 5     if measles1_listing == 1

	* drop babies that did not receive a vaccine, therefore could not have received a bracelet:
	drop if num_vacc==0



	* ----------------------------------------------------------------------------
	* Generate the indicators for the correct Implementation of signals:

	replace color_brac_listing="3"  if intervention_arm==1

	gen     yellow = 1              if inlist(color_brac_listing,"1")
	replace yellow = 0              if inlist(color_brac_listing,"2","2 3","1 2","3")
	gen     green  = 1              if inlist(color_brac_listing,"2","1 2","2 3")
	replace green  = 0              if inlist(color_brac_listing,"1","3")
	gen     nobracelet = 1          if inlist(color_brac_listing,"3")
	replace nobracelet = 0          if inlist(color_brac_listing,"1","1 2","2 3","2")

	gen     bracelet = 0            if inlist(intervention_arm,2,3,4)
	replace bracelet = 1            if yellow == 1|green == 1


	** Generate an indicator for each vaccine
	tab     num_vacc, gen(vac_num)
	
	foreach num of numlist 2 3 4 5  {
		lab var vac_num`num' "Vaccine `num'"
		lab val vac_num`num' vac_num`num'
	}
	
	lab var green "Green"
	lab val green green
	lab var yellow "Yellow"
	lab val yellow yellow



	save "${Replicate_SocialSignals_dtaFinal}/Listing_Data_BraceletHandout.dta", replace

	use  "${Replicate_SocialSignals_dtaFinal}/Listing_Data_BraceletHandout.dta", clear


	summ  green yellow nobracelet bracelet

	summ  nobracelet    if intervention_arm >1


	* Generate the means of bracelets recorded by vaccine and intervenation arm:
	bys num_vacc intervention_arm: egen mean_green = mean(green)
	bys num_vacc intervention_arm: tab  mean_green
	bys          intervention_arm: tab  mean_green

	bys num_vacc intervention_arm: egen mean_yellow = mean(yellow)
	bys num_vacc intervention_arm: tab  mean_yellow

	bys          intervention_arm: tab  mean_yellow
	bys num_vacc intervention_arm: egen mean_nobra = mean(nobracelet)
	bys num_vacc intervention_arm: tab  mean_nobra

	bys          intervention_arm: tab  mean_nobra

	bys          intervention_arm: sum  mean_nobra

	egen    sum = rowtotal(mean_green mean_yellow mean_nobra)
	replace sum = round(sum,.01)



	foreach treatment of numlist 1(1)4 {
		summ    mean_green mean_yellow mean_nobra     if intervention_arm == `treatment'
		local   Obs_`treatment' = trim("`: display %-9.0gc r(N)'")
	}
	summ    intervention_arm, detail
	local   Obs = trim("`: display %-9.0gc r(N)'")



	duplicates drop num_vacc intervention_arm, force


	summ       mean_green mean_yellow mean_nobra if intervention_arm == 3 & num_vacc == 4

	summ       mean_green mean_yellow mean_nobra if intervention_arm == 4 & num_vacc == 5


	* ----------------------------------------------------------------------------
	* Graph the correct implementation:

	cap graph drop _all


	* Graph: Control
	graph bar (asis) mean_green mean_yellow mean_nobra if intervention_arm==1,    ///
	over(num_vacc, sort(num_vacc)) stack                         ///
	bar(1, color(green))                                         ///
	bar(2, color(sandb))                                         ///
	bar(3, color(gs10))                                          ///
	ytick(0(0.1)1) nolabel  graphregion(color(none))             ///
	title("Control") subtitle("")   ytitle("Share of Children")  ///
	legend(order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received"))  ///
	name(Arm1)

	* Graph: Uninformative Bracelet
	graph bar (asis) mean_green mean_yellow mean_nobra if intervention_arm==2,    ///
	over(num_vacc, sort(num_vacc)) stack                          ///
	bar(1, color(green)) nolabel                                  ///
	bar(2, color(sandb))                                          ///
	bar(3, color(gs10))                                           ///
	ytick(0(0.1)1) graphregion(color(none))  ytitle()             ///
	title("Uninformative Bracelet") subtitle( "")                 ///
	legend(cols(3) order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received"))  ///
	name(Arm2)
									


	* Graph: Signal at 4th
	graph bar (asis) mean_green mean_yellow mean_nobra  if intervention_arm==3,   ///
	over(num_vacc, sort(num_vacc))  stack                         ///
	bar(1, color(green))                                          ///
	bar(2, color(sandb))                                          ///
	bar(3, color(gs10))                                           ///
	ytick(0(0.1)1)  graphregion(color(none)) subtitle("")         ///
	title("Signal at 4") ytitle("Share of Children") b1title("Vaccine") ///
	legend(order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received"))  ///
	name(Arm3)


	* Graph: Signal at 5th
	graph bar (asis) mean_green mean_yellow mean_nobra  if intervention_arm==4,   ///
	over(num_vacc, sort(num_vacc))   stack                        ///
	bar(1, color(green))                                          ///
	bar(2, color(sandb))                                          ///
	bar(3, color(gs10))                                           ///
	ytick(0(0.1)1)  graphregion(color(none)) subtitle("")         ///
	title("Signal at 5")  b1title("Vaccine") ytitle("")           ///
	legend(order(1 "Green bracelet" 2 "Yellow bracelet"  3 "No bracelet received") cols(3))   ///
	name(Arm4)


	* Combine all Graphs
	grc1leg  Arm1 Arm2 Arm3 Arm4,       ///
	legendfrom(Arm4)           ///
	graphregion(color(none))
	graph export "${Replicate_SocialSignals_Figures}/Figure_CorrectBraceletHandout.jpg", replace



* ******************************************************************************
* End of the Dofile !!!
* ******************************************************************************
